﻿using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using Newtonsoft.Json;

namespace Vepa.TestClient
{
    class Program
    {
        static TcpClient client;

        static void Main(string[] args)
        {
            getList();
            Console.ReadLine();
        }


        static void getList()
        {
            try
            {
                Console.WriteLine("Opening TCP Client..");
                client = new TcpClient("127.0.0.1", 7879);

                NetworkStream stream = client.GetStream();

                StreamReader reader = new StreamReader(stream);
                StreamWriter writer = new StreamWriter(stream);

                Console.WriteLine("Sending ListRequest..");

                Vepa.Shared.Networking.ListRequest request = new Vepa.Shared.Networking.ListRequest();
                
                writer.WriteLine(JsonConvert.SerializeObject(request));
                writer.Flush();


                Console.WriteLine("Receiving answer...");
                byte[] buffer = new byte[32];
                StringBuilder stringBuilder = new StringBuilder();
                int numberOfBytes = 0;
                do
                {
                    numberOfBytes = stream.Read(buffer, 0, buffer.Length);
                    stringBuilder.AppendFormat("{0}", Encoding.ASCII.GetString(buffer, 0, numberOfBytes));
                }
                while (stream.DataAvailable);

                Vepa.Shared.Networking.ListResponse response = JsonConvert.DeserializeObject<Vepa.Shared.Networking.ListResponse>(stringBuilder.ToString());

                foreach( Vepa.Shared.VepaInfo info in response.List)
                {
                    Console.WriteLine(string.Format("Version: {0}   Release: {1}",info.Version,info.ReleaseDateTime.ToLongDateString()));
                }              


                reader.Close();
                writer.Close();
                client.Close();

                Console.WriteLine("Finished...");

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());

            }
            System.Threading.Thread.Sleep(10000);

            Console.WriteLine("Press a key to exit.");
            Console.ReadLine();
        }


        static void getVepa()
        {


            try
            {
                Console.WriteLine("Opening TCP Client..");
                client = new TcpClient("127.0.0.1", 7879);

                NetworkStream stream = client.GetStream();

                StreamReader reader = new StreamReader(stream);
                StreamWriter writer = new StreamWriter(stream);

                Console.WriteLine("Sending VepaRequest..");
                Vepa.Shared.Networking.VepaRequest request = new Shared.Networking.VepaRequest();
                request.Version = 0;

                writer.WriteLine(JsonConvert.SerializeObject(request));
                writer.Flush();


                Console.WriteLine("Receiving answer...");
                byte[] buffer = new byte[32];
                StringBuilder stringBuilder = new StringBuilder();
                int numberOfBytes = 0;
                do
                {
                    numberOfBytes = stream.Read(buffer, 0, buffer.Length);
                    stringBuilder.AppendFormat("{0}", Encoding.ASCII.GetString(buffer, 0, numberOfBytes));
                }
                while (stream.DataAvailable);

                Vepa.Shared.Networking.VepaResponse response = JsonConvert.DeserializeObject<Vepa.Shared.Networking.VepaResponse>(stringBuilder.ToString());

                Console.WriteLine("Writing file");
                FileStream fs = new FileStream(response.Vepa.Info.Version + ".pdf", FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(Convert.FromBase64String(response.Vepa.Data));
                fs.Close();

                reader.Close();
                writer.Close();
                client.Close();

                Console.WriteLine("Finished...");

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());

            }
            System.Threading.Thread.Sleep(10000);

            Console.WriteLine("Press a key to exit.");
            Console.ReadLine();
        }

    }
}
